package goldrushcontroller

import (
	"context"
	"fmt"
	"gitee.com/go-mid/booter/bootservice"
	"gitee.com/go-mid/booter/web"
	"gitee.com/go-mid/infra/xcontext"
	"gitee.com/go-mid/infra/xlog"
	"gitee.com/xserv/goldrush/internal/component"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"net/http"
	"time"
)

//原始请求 demo
func DemoRaw(c *bootservice.Context) {
	fun := "DemoController.GetAgentInfo -->"
	ctx := xcontext.NewValueContext(c.Request.Context())
	xlog.Infof(ctx, "%s ", fun)
	var res = map[string]interface{}{
		"aaa": 1,
	}
	c.JSON(200, res)
}

//原始请求，自定义 绑定入参解析器。
//参考gin文档 https://gin-gonic.com/docs/examples/binding-and-validation/
func DemoForm2Request(c *bootservice.Context) {
	fun := "DemoController.GetAgentInfo -->"
	ctx := xcontext.NewValueContext(c.Request.Context())
	xlog.Infof(ctx, "%s ", fun)
	var obj DemoFormRequest
	if err := c.ShouldBind(&obj); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}

	c.JSON(200, gin.H{"data": obj})
}

// 原始请求，已解析请求头、登录态等 demo
// 自定义响应了字符串
type DemoXfrmRequest struct {
}

func FactoryDemoXfrmRequest() web.HandleRequest {
	return new(DemoXfrmRequest)
}
func (m *DemoXfrmRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	//
	c.String(200, "测试纯文本响应：%d", time.Now().UnixNano())
	//注意 在函数里一旦  render过，这里就需要终止了
	c.Abort()
	return nil, nil
}

// 测试各种响应, 请求是json的
type DemoWriterRequest struct {
	Ttype string `json:"ttype"`
}

func FactoryDemoWriterRequest() web.HandleRequest {
	return new(DemoWriterRequest)
}
func (m *DemoWriterRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	//
	switch m.Ttype {
	case "text":
		c.String(http.StatusOK, "测试纯文本响应：%d", time.Now().UnixNano())
	case "json":
		c.JSON(http.StatusOK, map[string]interface{}{
			"test1": 1,
		})

	case "xml":
		c.XML(http.StatusOK, gin.H{"wechat": "flysnow_org", "blog": "www.flysnow.org"})
	case "yaml":
		c.YAML(http.StatusOK, map[string]interface{}{
			"test1": 1,
			"obj": map[string]interface{}{
				"o1": 1,
			},
		})
	case "redirect":
		c.Redirect(http.StatusFound, "https://www.ipalfish.com")
		//跳转 必须把响应写出去
		c.Writer.WriteHeaderNow()
	}
	//注意 在函数里一旦  render过，这里就需要终止了
	c.Abort()
	return nil, nil
}

// 文件上传，已解析请求头、登录态等 demo
type DemoFileRequest struct {
}

func FactoryDemoFileRequest() web.HandleRequest {
	return new(DemoFileRequest)
}
func (m *DemoFileRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {

	ffile, err := c.FormFile("data")
	if err != nil {
		xlog.Warnf(ctx, " do something err:   err: %v", err)
		return nil, status.Error(codes.Internal, err.Error())
	}
	return map[string]interface{}{
		"len": ffile.Size,
	}, nil
}

// 多个文件上传，已解析请求头、登录态等 demo
type DemoMulFileRequest struct {
}

func FactoryDemoMulFileRequest() web.HandleRequest {
	return new(DemoMulFileRequest)
}
func (m *DemoMulFileRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	//
	form, _ := c.MultipartForm()
	files := form.File["data"]
	var res = make(map[string]interface{})
	for i, f := range files {
		res[fmt.Sprintf("len %d: ", i)] = f.Size
	}
	return res, nil
}

// 测试表单方式的请求， query中参数等
//
type DemoFormRequest struct {
	//Id int64 `form:"id" binding:"required"`
	Id   int64  `form:"id"`
	Name string `form:"name"`
}

func FactoryDemoFormRequest() web.HandleRequest {
	return new(DemoFormRequest)
}
func (m *DemoFormRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	//
	test := c.Query("test")
	page := c.DefaultQuery("page", "1111")
	return map[string]interface{}{
		"id":   m.Id,
		"test": test,
		"page": page,
		"name": m.Name,
	}, nil
}

// 测试 取配置信息
type DemoConfRequest struct {
	Key string `json:"key"`
}

func FactoryDemoConfRequest() web.HandleRequest {
	return new(DemoConfRequest)
}
func (m *DemoConfRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	v, ok := component.Sbase.ConfigCenter(ctx).GetString(ctx, "application", m.Key)
	return map[string]interface{}{
		"v":  v,
		"ok": ok,
	}, nil
}

// 测试登录态
type DemoLoginRequest struct {
}

func FactoryDemoLoginRequest() web.HandleRequest {
	return new(DemoLoginRequest)
}
func (m *DemoLoginRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	return map[string]interface{}{
		"clientInfo": clientInfo,
		"authInfo":   authInfo,
	}, nil
}
